<!DOCTYPE html>
<html  style="overflow : hidden; ">
<head>
    <title>地理位置标注</title>
    <meta name="keywords" content="矢量瓦片,矢量索引">
    <meta name="description" content="矢量瓦片">
    <meta name="content-type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width,target-densitydpi=high-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <link rel="stylesheet" href="../sdk_web/LeafletSDK_es6/css/leaflet.css" />
    
    <style>
        button, input {
            position: absolute;
            left: 10px;
            z-index: 100000;
            outline: none;
            border: none;
            background: white;
            font-size: 14px;
        }
        .resetBtn {
            bottom: 30%;
            border: 1px solid rgb(51, 136, 255);
            width: 32px;
            height: 32px;
            border-radius: 2px;
            background: rgba(255, 255, 255, 0.9);
            padding: 0;
            margin: 0;
        }
        .modifyBtn {
            bottom: 20px;
            width: calc(100% - 20px);
            line-height: 36px;
            box-shadow: 0px 0px 14px -1px #c8c8c8;
        }
        .iptSearch {
            width: calc(100% - 55px);
            padding-left: 25px;
            padding-right: 10px;
            top: 10px;
            line-height: 36px;
            box-shadow: 0px 0px 14px -1px #c8c8c8;
            background: url("../img/search.png") no-repeat 5px center #ffffff;
        }
    </style>
</head>

<body style="margin : 0; ">
    <div id="map" class="map" style ="position:absolute;bottom:0px;top:0px;width:100%"></div>
    <input type="text" placeholder="请输入地址" class="iptSearch" />
    <button class="resetBtn">
        <img src="../img/location.png" style="height: 30px;" alt="">
    </button>
    <button class="modifyBtn">标注</button>

    <script src="../sdk_web/LeafletSDK_es6/dist/leaflet-src.js"></script>
    <script src="../sdk_web/LeafletSDK_es6/dist/leaflet.js"></script>
    <script src="../sdk_web/LeafletSDK_es6/dist/CustomWebSDK.min.js"></script> 
    <script src="../libs/jquery-1.12.3.min.js"></script>
    <!-- <script src="//code.jquery.com/jquery-3.2.1.min.js"></script> -->
    <script src="https://appx/web-view.min.js"></script>
    <script>
        if (navigator.userAgent.indexOf('AlipayClient') > -1) {
          document.writeln('<script src="https://appx/web-view.min.js"' + '>' + '<' + '/' + 'script>');
        }
        my.getEnv(function(res) {
            if (!res.miniProgram) {
                my.alert({
                    content:JSON.stringify('运行出错')
                });
            }
        });
        var initLatlng, initZoom = 17, cityName, newCenterData = {};
        
        my.postMessage('init');
        // 来自小程序的消息。
        my.onMessage = function(e) {
            initLatlng = {lon: e.lon, lat: e.lat} || {lon: 120.14989, lat: 30.27751};  // 默认经纬度为蓝天商务中心
            cityName = e.cityName || "杭州市";
            // my.alert({
            //     content: JSON.stringify(cityName)
            // }); // 测试是否接收到小程序中传来的数据
            $.ajax({
                url: encodeURI("http://dh.ditu.zj.cn:18005/inverse/getInverseGeocoding.jsonp?&detail=1&zoom=11&latlon=" + initLatlng.lon + "," + initLatlng.lat + "&lat=&lon=&customer=2"),
                dataType: "jsonp",
                // jsonp: "callback",
                success: function(res) {
                    newCenterData.latitude = parseFloat(res.latlon.split(',')[1])
                    newCenterData.longitude = parseFloat(res.latlon.split(',')[0])
                    newCenterData.location = res.city.value + res.dist.value + res.town.value + res.poi;
                },
                error: function (err) {
                    my.alert({
                        content: "地址解析出错"
                    });
                }
            });
            init()
        }
        function init() {
            var map = L.map('map',{crs:L.CRS.CustomEPSG4326,center: initLatlng, minZoom: 5, zoom: initZoom, inertiaDeceleration:15000, zoomControl: false});
            var tileAddress = 'http://ditu.zjzwfw.gov.cn/mapserver/vmap/zjvmap/getMAP?x={x}&y={y}&l={z}&styleId=tdt_biaozhunyangshi_2017';

            var layer = new L.GXYZ(tileAddress, {tileSize:512,attribution: '&copy; <a href="http://www.icinfo.cn/">http://www.icinfo.cn</a> contributors',minZoom: 5});
            map.attributionControl.setPrefix('汇信科技');
            map.addLayer(layer);

            // 添加注记图层
            var labelLayer = new L.GWVTAnno({tileSize:512});
            var dataSource = new Custom.URLDataSource();
            dataSource.url = 'http://ditu.zjzwfw.gov.cn/mapserver/label/zjvmap/getDatas?x=${x}&y=${y}&l=${z}&styleId=tdt_biaozhunyangshi_2017';
            labelLayer.addDataSource(dataSource);
            map.addLayer(labelLayer);
            var customIcon = L.icon({ 
                iconUrl: '../img/indoor_pub_poi_pressed.png',
                iconSize: [21, 30],
                iconAnchor:   [10, 20],
            }); 
            var marker = L.marker( 
                [map.getCenter().lat, map.getCenter().lng], 
                { 
                    draggable: false,// 使图标可拖拽
                    opacity: 1,
                    icon: customIcon//添加自定义的图标
                }
            ).addTo(map);
            // marker.bindTooltip("my tooltip text").openTooltip();
            L.circle([map.getCenter().lat, map.getCenter().lng], {radius: 30}).addTo(map);

            map.on('click', function(e) {
                var reverseResolutionUrl = encodeURI("http://dh.ditu.zj.cn:18005/inverse/getInverseGeocoding.jsonp?&detail=1&zoom=11&latlon=" + e.latlng.lng + "," + e.latlng.lat + "&lat=&lon=&customer=2");
                $.ajax({
                    url: reverseResolutionUrl,
                    dataType: "jsonp",
                    // jsonp: "callback",
                    success: function(res) {
                        newCenterData.latitude = parseFloat(res.latlon.split(',')[1])
                        newCenterData.longitude = parseFloat(res.latlon.split(',')[0])
                        newCenterData.location = res.city.value + res.dist.value + res.town.value + res.poi;
                        marker.setLatLng(e.latlng);
                        marker.unbindTooltip().bindTooltip(res.city.value + res.dist.value + res.town.value + res.poi, {offset: [0, 10], direction : "bottom"}).openTooltip();
                    },
                    error: function (err) {
                        my.alert({
                            content: "地址解析出错"
                        });
                    }
                });
            });

            $('.iptSearch').on('keydown',function(e){
                // e.preventDefault();
                if(e.keyCode == 13){
                    if(e.target.value) {
                        var keyWord = cityName + e.target.value;
                        var resolutionUrl = encodeURI("http://dh.ditu.zj.cn:18006/geocoding/getLatLonByAddress.jsonp?&city=" + cityName + "&keyword=" + keyWord + "&width=500&height=430&pn=1&customer=2&encode=UTF-8");
                        $.ajax({
                            url: resolutionUrl,
                            dataType: "jsonp",
                            // jsonp: "callback",
                            success: function(res) {
                                if (res.strlatlon == "0.0,0.0") {
                                    my.alert({
                                        content: "未查询到相关信息"
                                    });
                                } else {
                                    newCenterData.latitude = parseFloat(res.strlatlon.split(',')[1]);
                                    newCenterData.longitude = parseFloat(res.strlatlon.split(',')[0]);
                                    newCenterData.location = res.city + res.keyword;
                                    var newCenterLatlon = {lon: newCenterData.longitude, lat: newCenterData.latitude}
                                    marker.setLatLng(newCenterLatlon);
                                    marker.unbindTooltip().bindTooltip(keyWord, {offset: [0, 10], direction : "bottom"}).openTooltip();
                                    map.panTo(newCenterLatlon)
                                }
                            },
                            error: function (err) {
                                my.alert({
                                    content: "地址解析出错"
                                });
                            }
                        });
                    } else {
                        my.alert({
                            content: "请输入查询关键字"
                        });
                    }
                    
                }
            });
            $('.resetBtn').on('click',function(e){
                map.setView([Number(initLatlng.lat), Number(initLatlng.lon)], initZoom);
                marker.unbindTooltip().setLatLng(initLatlng);
            });
            $(".modifyBtn").on('click', function () {
                my.postMessage(newCenterData);
            })

        }
        

    </script>

</body>
</html>
